package com.classroommanagesystem.utils;

/**
 * @author victory
 * @date 2025/10/26 16:48
 */

import com.classroommanagesystem.common.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 工具类，用于向响应写入错误信息
 */
public class ErrorResponseUtil {
    // 用于将Result转换为JSON
    private static final ObjectMapper objectMapper = new ObjectMapper();

    // 工具方法,向响应写入错误信息
    public static  void write(HttpServletResponse response, String message) throws IOException {
        response.setContentType("application/json;charset=UTF-8"); // 设置响应格式为JSON
        Result errorResult = Result.error(message); // 构建错误结果
        response.getWriter().write(objectMapper.writeValueAsString(errorResult)); // 转换为JSON并写入响应
    }
}
